package com.liliudong.cursormate.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liliudong.cursormate.common.R;
import com.liliudong.cursormate.common.exception.BusinessException;
import com.liliudong.cursormate.entity.SysRole;
import com.liliudong.cursormate.service.SysRolePermissionService;
import com.liliudong.cursormate.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 用户角色控制器
 *
 * @author Liudong.li
 * @version 1.0
 * @project cursor-mate
 * @date 2023/10/25 10:10:36
 */
@RestController
@RequestMapping("/v1/sys-role")
@RequiredArgsConstructor
public class SysRoleController {

    private final SysRoleService sysRoleService;

    private final SysRolePermissionService sysRolePermissionService;

    /**
     * 添加角色
     *
     * @param sysRole sys角色
     * @return {@link R}<{@link ?}>
     */
    @PostMapping
    @Transactional
    @SaCheckPermission("sys:role:add")
    public R<?> create(@RequestBody SysRole sysRole) {
        long count = sysRoleService.count(
                Wrappers.lambdaQuery(SysRole.class)
                        .eq(SysRole::getRoleCode, sysRole.getRoleCode())
        );
        if (count > 0) {
            throw new BusinessException("角色编码已存在");
        }
        // 保存角色信息
        sysRoleService.save(sysRole);
        // 保存角色权限信息
        sysRolePermissionService.saveBatchByRoleId(sysRole.getId(), sysRole.getSysPermissionList());
        return R.success();
    }

    /**
     * 根据ID删除
     *
     * @param id id
     * @return {@link R}<{@link ?}>
     */
    @Transactional
    @DeleteMapping("/{id}")
    @SaCheckPermission("sys:role:del")
    public R<?> del(@PathVariable Long id) {
        sysRoleService.removeById(id);
        sysRolePermissionService.removeByRoleId(id);
        return R.success();
    }

    /**
     * 更新
     *
     * @param sysRole sys角色
     * @return {@link R}<{@link ?}>
     */
    @PutMapping
    @Transactional
    @SaCheckPermission("sys:role:edit")
    public R<?> update(@RequestBody SysRole sysRole) {
        sysRoleService.updateById(sysRole);
        // 删除角色权限信息
        sysRolePermissionService.removeByRoleId(sysRole.getId());
        // 保存角色权限信息
        sysRolePermissionService.saveBatchByRoleId(sysRole.getId(), sysRole.getSysPermissionList());
        return R.success();
    }

    /**
     * 页
     *
     * @param page     页
     * @param pageSize 页面大小
     * @return {@link R}<{@link ?}>
     */
    @GetMapping("/page")
    @SaCheckPermission("sys:role:page")
    public R<Page<SysRole>> page(@RequestParam(defaultValue = "1") Long page,
                                 @RequestParam(defaultValue = "10") Long pageSize) {
        Page<SysRole> sysRolePage = sysRoleService.page(Page.of(page, pageSize));

        return R.success(sysRolePage);
    }

    /**
     * 详情信息
     *
     * @param id id
     * @return {@link R}<{@link SysRole}>
     */
    @GetMapping("/{id}")
    @SaCheckPermission("sys:role:info")
    public R<SysRole> info(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getByIdIncludePermissions(id);
        return R.success(sysRole);
    }
}
